﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BG_Manage_BG_Placard : System.Web.UI.Page
{
    VPINews.BLL.Placard bll_card = new VPINews.BLL.Placard();
    VPINews.Model.Placard mod_card = new VPINews.Model.Placard();
    private static int cardid;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getPlacard();
        }
    }

    protected void getPlacard()
    {
        GridView1.DataSource = bll_card.GetAllList();
        GridView1.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string Contents = this.txtContents.Value.Trim();
            DateTime PubDate = DateTime.Now;

            int cardid = Convert.ToInt32(hdID.Value);

            if (cardid > 0)
            {
                mod_card.CardID = cardid;
                mod_card.Contents = Contents;
                mod_card.PubDate = PubDate;
                bll_card.Update(mod_card);
                smslabel.Text = "公告修改成功";

                txtContents.Value = "";
                hdID.Value = "0";

                labshow.Text = "添加公告";
                btnAdd.Text = "· 添加 ·";
            }

            else
            {
                mod_card.Contents = Contents;
                mod_card.PubDate = PubDate;
                bll_card.Add(mod_card);
                smslabel.Text = "公告添加成功";
                txtContents.Value = "";               
            }

            getPlacard();
        }

    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtContents.Value = "";
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = bll_card.GetAllList();
        GridView1.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteCard")
        {
            bll_card.Delete(Convert.ToInt32(e.CommandArgument));
            getPlacard();
        }
        if (e.CommandName == "EditCard")
        {
            VPINews.BLL.Placard bll = new VPINews.BLL.Placard();
            VPINews.Model.Placard mod = bll.GetModel(Convert.ToInt32(e.CommandArgument));
            labshow.Text="修改公告";
            btnAdd.Text="· 修改 ·";
            this.txtContents.Value = mod.Contents;
            cardid = mod.CardID;
            this.hdID.Value = cardid.ToString();
        }

    }
  
}
